package com.miya.order.configure.rabbitmq.dead;

import com.rabbitmq.client.Channel;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;

import java.io.IOException;
import java.nio.charset.StandardCharsets;

/**
 * 死信消息接收器
 *
 * @author Caixiaowei
 * @date 2022/02/21
 */
@Slf4j
@Component
public class DeadLetterMessageReceiver {

	@RabbitListener(queues = DeadLetterDemoConfigure.DEAD_LETTER_QUEUEA)
	public void receiveA(Message message, Channel channel) throws IOException {
		long deliveryTag = message.getMessageProperties().getDeliveryTag();
		String messageStr = new String(message.getBody(), StandardCharsets.UTF_8);
		log.info("receiveA: {}", messageStr);
		// 处理发生死信的具体业务逻辑

		channel.basicAck(deliveryTag, false);
	}

	@RabbitListener(queues = DeadLetterDemoConfigure.DEAD_LETTER_QUEUEB)
	public void receiveB(Message message, Channel channel) throws IOException {
		long deliveryTag = message.getMessageProperties().getDeliveryTag();
		String messageStr = new String(message.getBody(), StandardCharsets.UTF_8);
		log.info("receiveB: {}", messageStr);
		// 处理发生死信的具体业务逻辑

		channel.basicAck(deliveryTag, false);
	}
}
